package com.imu.wys.dao;

import com.imu.wys.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;


@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User findByUserName(String username){
        String sql = "select * from j1_user where username = ?";
        // 创建一个新的BeanPropertyRowMapper对象
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        try {
            return jdbcTemplate.queryForObject(sql,new Object[]{username},rowMapper);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

    }


    public int insertUser(User user){
        String sql = "insert into j1_user(username,password) values (?,?)";
        // 创建一个新的BeanPropertyRowMapper对
        return jdbcTemplate.update(sql,user.getUsername(),user.getPassword());
    }

}
